# -*- coding:UTF-8 -*-
from cmdb.models import BusinessInfo
from django.db.models import Q

import string

def business_info(category,search,start,end):
    count = 0
    bRes = {}
    if search == '':
        if category == 0:
            count=BusinessInfo.objects.count()
            bRes = BusinessInfo.objects.order_by('-id')[start:end]
        else:
            count=BusinessInfo.objects.filter(category=category).count()
            bRes = BusinessInfo.objects.filter(category=category).order_by('-id')[start:end]
    else:
        if category == 0:
            count=BusinessInfo.objects.filter(module_name__icontains=search).count()
            bRes = BusinessInfo.objects.filter(module_name__icontains=search).order_by('-id')[start:end]
        else:
            count=BusinessInfo.objects.filter(Q(category=category),Q(module_name__icontains=search)).count()
            bRes = BusinessInfo.objects.filter(Q(category=category),Q(module_name__icontains=search)).order_by('-id')[start:end]
    return {'count':count,'business_info_list':bRes}

def filter_business_info(module_no):
    businessInfo = BusinessInfo.objects.get(module_no=module_no)
    return businessInfo

def exist_category_business_info(category):
    count = BusinessInfo.objects.filter(category=category).count()
    if count > 0:
        return True
    return False

def add_business_info(module_name, category, project, application, comment, operator, responser, responser_back, cursor):
    module_no = 'BU00001'
    module_no_sql = "select module_no from business_info order by module_no desc limit 1"
    cursor.execute(module_no_sql)
    bqNumRes = cursor.fetchone()
    if bqNumRes:
        tmpNo=str(int(bqNumRes[0][2:]) + 1)
        module_no = 'BU' + string.rjust(tmpNo,5,'0')
    business_insert_sql = 'insert into business_info set module_no="' + module_no + '",module_name="' + module_name \
                          + '",category=' + category + ',project="' + project + '",application="' + application \
                          + '",comment="' + comment + '",operator="' + operator + '",responser="' + responser \
                          + '",responser_back="' + responser_back + '"'
    cursor.execute(business_insert_sql)
    return module_no

def get_business_info(module_no):
    bInfo = BusinessInfo.objects.get(module_no=module_no)
    return bInfo

def update_business_info(id, comment,operator, responser, responser_back, cursor):
    business_update_sql = 'update business_info set comment="' + comment + '",operator="' + operator \
                          + '",responser="' + responser + '",responser_back="' + responser_back \
                          + '" where id=' + id
    cursor.execute(business_update_sql)

def del_business_info(id):
    BusinessInfo.objects.get(id=id).delete()

def exist_business_info(category,module_name):
    count = BusinessInfo.objects.filter(Q(category=category),Q(module_name=module_name)).count()
    if count > 0:
        return True
    return False

